package com.da.javalog.check.core;

import java.util.LinkedList;

/**
 * @author joker
 * @date 2020/04/28
 */
public class CheckerChain<CheckContext> implements Checker<CheckContext> {
    private LinkedList<Checker> checkerList = new LinkedList<>();

    public CheckerChain addLast(Checker checker) {
        this.checkerList.addLast(checker);
        return this;
    }

    public CheckerChain addFirst(Checker checker) {
        this.checkerList.addFirst(checker);
        return this;
    }

    @Override
    public void check(CheckContext checkContext, CheckerChain<CheckContext> checkChain) throws CheckException {
        if (checkerList.isEmpty()) {
            return;
        }
        Checker checker = checkerList.removeFirst();
        checker.check(checkContext, checkChain);
    }
}
